package com.sky.mapper;

import com.sky.entity.OrderEntity;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    @Insert("insert into orders (number,status,user_id,address_book_id,order_time,checkout_time,pay_method,pay_status,amount,remark,phone,address,user_name,consignee,cancel_reason,rejection_reason,cancel_time,estimated_delivery_time,delivery_status,delivery_time,pack_amount,tableware_number,tableware_status) " +
            "values (#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(OrderEntity entity);

    @Select("select * from orders where id = #{id}")
    OrderEntity getById(Long id);

    List<OrderEntity> pageQuery(OrderEntity entity);

    void update(OrderEntity entity);

    @Select("select * from orders where number = #{number} and user_id = #{userId}")
    OrderEntity getOrderByNumberAndUserId(@Param("number") String number, @Param("userId") Long userId);

    @Select("select * from orders where status = #{status} and TIMESTAMPDIFF(MINUTE, order_time,NOW()) > 15")
    List<OrderEntity> getOrderByStatusAndOrderTimeWith15MinuteAgo(@Param("status") Integer status);

    @Select("select from orders where status = #{status} and order_time < #{time}")
    List<OrderEntity> getOrderByStatusAndOrderTimeLT(@Param("status") Integer status, @Param("time") LocalDateTime time);

    void updateBatchByStatus(@Param("status") Integer status, @Param("orderIds") List<Long> orderIds);
}
